require 'env'
require 'logger'

module <%= class_name %>Base

  RAPP_VERSION="<%= rapp_version %>"

  def self.included(base) #:nodoc:
    base.extend(ClassMethods)
    <%= class_name%>Base.boot!
  end

  module ClassMethods
    def env
      @env ||= <%=class_name%>::Env.new(ENV['APP_ENV'] ||= 'development')
    end

    def root
      File.expand_path("..", File.dirname(__FILE__))
    end

    def logger
      @logger ||= ::Logger.new(ENV['APP_LOG_PATH'] || "./log/<%= app_name %>.log").tap do |l|
        l.level = ::Logger::DEBUG
        l.formatter = lambda do |severity, datetime, progname, msg|
           "[#{datetime} (#{Process.pid})] #{severity} : #{msg}\n"
        end
      end
    end
  end

  def self.boot!
    # Load all dependent gems
    require 'bundler'
    Bundler.require(:default, <%=class_name %>.env.to_s)

    # Set up additional load paths

    # I'm not sure if we need "app" in the load path
    # Everything there should be auto-loaded, but you never know...
    $:.unshift File.expand_path("./app")

    # Load the right environment initializer

    require "config/environments/#{<%=class_name %>.env.to_s}"

    # Load initializers

    Dir["./config/initializers/*.rb"].sort.each {|file| require file }

    # Load config

    Dir["./config/**/*.rb"].sort.each {|file| require file}

    # Load job files

    Dir["./app/models/**/*.rb"].sort.each {|file| require file }
    Dir["./app/services/**/*.rb"].sort.each {|file| require file }
    Dir["./app/jobs/**/*.rb"].sort.each {|file| require file }
    end
end
